﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HotelBookingSystem
{
    public class Menu
    {
        public static void MenuInterface()
        {
            HotelManager hotelManager = new HotelManager();
            Clients clients = new Clients();
            ConsoleMenuInput iw = new ConsoleMenuInput();
            Console.Clear();
            DrawLogo();
            hotelhelp();
            string cmd;
            Console.WriteLine("Please, enter command!");
            cmd = iw.getString("Command> ");
            while (!cmd.Equals("quit"))
            {
                try
                {
                    if (cmd.Equals("hotels"))
                    {
                        string city = iw.getString("city: ");
                        hotelManager.ShowHotels(city);
                    }
                    else if (cmd.Equals("all"))
                        hotelManager.ShowHotels();
                    else if (cmd.Equals("cities"))
                        hotelManager.ShowCities();
                    else if (cmd.Equals("add"))
                    {
                        string city = iw.getString("city: ");
                        string name = iw.getString("name: ");
                        int rooms = iw.getInt("rooms: ");
                        decimal rate = iw.getDecimal("rate: ");
                        string comment = hotelManager.AddHotel(city, name, rooms, rate);
                        if (comment == "OK")
                            Console.WriteLine("Hotel {0} has been added", name);
                        else
                            Console.WriteLine(comment);
                    }
                    else if (cmd.Equals("book"))
                    {
                        int customerId = iw.getInt("customer Id: ");
                        string city = iw.getString("city: ");
                        string name = iw.getString("hotel name: ");
                        string sdate = iw.getString("date: ");
                        DateTime date = DateTime.Parse(sdate);
                        int numDays = iw.getInt("number of days: ");
                        TypeBookable type =(TypeBookable) iw.getInt("room type? Enter 0 for Single Room, 1 for Double Room and 2 for Apartment: ");
                        
                        BookingResult result = hotelManager.Book(customerId, city, name,
                            date, numDays, type);
                        if (result.BookingId == -1)
                        {
                            Console.WriteLine("Reservation could not be booked");
                            Console.WriteLine(result.Comment);
                        }
                        else
                        {
                            //string typeOfRoom = iw.getString("Type of room (Single/Double/Apartment):    ");
                            Room testRoom = new SingleRoom(result.Rate);
                            switch (type)
                            {
                                case TypeBookable.singleRoom:
                                    testRoom = new SingleRoom(result.Rate);
                                    break;
                                case TypeBookable.doubleRoom:
                                    testRoom = new DoubleRoom(result.Rate);
                                    break;
                                case TypeBookable.apartment:
                                    testRoom = new Appartment(result.Rate);
                                    break;    
                                default:
                                    Console.WriteLine("You have entered wrong type of room");
                                    break;
                            }
                            string view = iw.getString("A room with view? YES/NO:    ");
                            if (view.ToLower() == "yes")
                            {
                                testRoom = new View(testRoom);
                            }
                            string smoker = iw.getString("Smoker? YES/NO:   ");
                            if (smoker.ToLower() == "yes")
                            {
                                testRoom = new Smoker(testRoom);
                            }
                            string miniBar = iw.getString("Mini Bar in the room? YES/NO:    ");
                            if (miniBar.ToLower() == "yes")
                            {
                                testRoom = new MiniBar(testRoom);
                            }
                            Console.WriteLine("Reservation has been booked");
                            Console.WriteLine("ReservationId = {0}", result.BookingId);
                            Console.WriteLine("ReservationRate = {0:C}", testRoom.Cost());
                            Console.WriteLine("ReservationCost = {0:C}",
                                testRoom.Cost()*numDays);
                            Console.WriteLine("You have reserved: {0}", testRoom.GetDescription());
                            Console.WriteLine("Comment = {0}", result.Comment);
                        }
                    }
                    else if (cmd.Equals("bookings"))
                    {
                        int customerId = iw.getInt("customer Id: ");
                        hotelManager.ShowBookings(customerId);
                    }
                    else if (cmd.Equals("register"))
                    {
                        string firstName = iw.getString("first name: ");
                        string lastName = iw.getString("last name: ");
                        string email = iw.getString("email address: ");
                        int id = clients.RegisterClient(firstName, lastName, email);
                        Console.WriteLine("id = {0}", id);
                    }
                    else if (cmd.Equals("email"))
                    {
                        int id = iw.getInt("customer id: ");
                        string email = iw.getString("email address: ");
                        clients.ChangeEmailAddress(id, email);
                    }
                    else if (cmd.Equals("show"))
                    {
                        int id = iw.getInt("customer id (-1 for all): ");
                        clients.ShowClients(id);
                    }
                    else
                        hotelhelp();
                }
                catch (Exception e)
                {
                    Console.WriteLine("Exception: {0}", e.Message);
                }
                cmd = iw.getString("H> ");

            }
        }
        private static void hotelhelp()
        {
            Console.WriteLine("The following commands are available:");
            Console.WriteLine("\thotels    shows all hotels in a city");
            Console.WriteLine("\tall       shows all hotels");
            Console.WriteLine("\tcities    shows all cities");
            Console.WriteLine("\tadd       adds a hotel");
            Console.WriteLine("\tbook      book a reservation");
            Console.WriteLine("\tbookings  show all bookings");
            Console.WriteLine("\tregister  register a customer");
            Console.WriteLine("\temail     change email address");
            Console.WriteLine("\tshow      show customers");
            Console.WriteLine("\tquit      exit the program");
        }
        public static void DrawLogo()
        {
            System.Console.WriteLine(" .d8888b. ");
            System.Console.WriteLine("d88P  Y88b");
            System.Console.WriteLine("Y88b. ");
            System.Console.WriteLine(" \"Y888b.      .d888888b.    .d88b.     .d88b.     .d88b.  Y8b.        db.");
            System.Console.WriteLine("    \"Y88b.    d88    88b   d88\"\"88b   d88\"\"88b   d88\"\"88b  \"Y8b.     d8p ");
            System.Console.WriteLine("      \"888    888    888   888  888   888  888   888  888    \"Y8b.  d8p  ");
            System.Console.WriteLine("Y88b  d88P    888    888   Y88..88P   Y88..88P   888  88P      \"88.d8p   ");
            System.Console.WriteLine(" \"Y8888P\"     888    888    \"Y88P\"     \"Y88P\"    888Y88P\"        d88P ");
            System.Console.WriteLine("                                                 888             d8P ");
            System.Console.WriteLine("                                                 888            d8P ");
            System.Console.WriteLine("                                                 888           d8P ");
            System.Console.WriteLine("                                                 888          d8P ");
        }
    }
}
